package com.scmt.hmzd.controller;
import cn.hutool.core.net.multipart.UploadFile;
import com.scmt.base.utils.BASE64DecodedMultipartFile;
import com.scmt.core.common.utils.ResultUtil;
import com.scmt.core.common.vo.Result;
import com.scmt.hmzd.utils.UploadFileUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.UnsupportedEncodingException;


@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/scmt/download")

public class FileController {
    @PostMapping(value = "/fileUpload")
    public Result<Object> fileUpload(@RequestParam(value = "file", required = false) MultipartFile multipartFile) {
        try {
            String upload = UploadFileUtils.uploadFile(multipartFile);
            return ResultUtil.data(upload);
        }catch (Exception e){
            return ResultUtil.error("文件上传失败！");
        }
    }
    /**
     * 功能描述：上传图片
     */
    @PostMapping("uploadImages")
    public Result<Object> uploadImages(@RequestBody String imgUrl) throws UnsupportedEncodingException {
        //base64 转文件
        MultipartFile imgFile = BASE64DecodedMultipartFile.base64ToMultipart(imgUrl);
        //文件存储在nginx代理路径下
        String fileName = UploadFileUtils.uploadFile(imgFile);
        return ResultUtil.data(fileName, "");
    }

    @GetMapping(value = "/fileDelete")
    public Result<Object> fileDelete(String url) {
        try {
            UploadFileUtils.deleteFile(url);
            return ResultUtil.success("1");
        }catch (Exception e){
            return ResultUtil.error("删除失败！");
        }
    }
}
